package com.regan.erp.util;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ObjectUtil;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.regan.erp.exception.MyBaselogicException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 静态文件
 *
 * @author zhangby
 * @date 2017/11/2 17:26
 */
public class Constants {
    private static Prop errprProp = null;

    public static final String SUCCESS = "success";
    public static final String FAIL = "fail";

    /**  错误码  */
    /**
     * 正常 code：000
     */
    public static final String NORMAL = "000";
    /**
     * 错误 code：999
     */
    public static final String ERROR = "999";


    /**  错误信息 */
    /**
     * 正常 code：000
     */
    public static final String MSG_000 = "成功";
    /**
     * 错误 code：999
     */
    public static final String MSG_999 = "系统错误";

    /**
     * yes_no: 0 否
     */
    public static final Integer YES_NO_0 = 0;
    /**
     * yes_no: 1 是
     */
    public static final Integer YES_NO_1 = 1;
    /**
     * yes_no: 0 否
     */
    public static final String YES_NO_TYPE_0 = "0";
    /**
     * yes_no: 1 是
     */
    public static final String YES_NO_TYPE_1 = "1";

    /**
     * 加载错误信息配置文件
     *
     * @return
     */
    public static Prop getPropInstance() {
        if (null == errprProp) {
            errprProp = PropKit.use("error.properties");
        }
        return errprProp;
    }

    /**
     * 获取错误代码
     *
     * @param num
     * @return
     */
    public static String error_code(String num) {
        return num;
    }

    /**
     * 获取错误信息
     *
     * @param num
     * @return
     */
    public static String error_msg(String num) {
        return getPropInstance().get("code_" + num);
    }



    /**
     * 获取异常堆栈里的全部信息
     *
     * @param e
     * @return
     */
    public static String getExceptionMsg(Exception e) {
        //获取异常堆栈信息
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        e.printStackTrace(new PrintWriter(buf, true));
        String expMessage = buf.toString();
        try {
            buf.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
//        String exceptionMsg = getExceptionMsg(e);
        String msg = "";
        if (e instanceof MyBaselogicException) {
            MyBaselogicException e1 = (MyBaselogicException) e;
            msg = Constants.error_msg(e1.getNum());
            if (ObjectUtil.isNotNull(e1.getMsg())) {
                msg = StrFormatter.format(msg, e1.getMsg());
            }
            msg = "MyBaselogicException -> 自定义异常[" + e1.getNum() + "]：" + msg + "\n";
        }
        return msg + expMessage;
    }

    /**
     * 获取自定义异常信息
     *
     * @param e
     * @return
     */
    public static String getMyExceptionMsg(Exception e) {
        String msg = "";
        if (e instanceof MyBaselogicException) {
            MyBaselogicException e1 = (MyBaselogicException) e;
            msg = Constants.error_msg(e1.getNum());
            if (ObjectUtil.isNotNull(e1.getMsg())) {
                msg = StrFormatter.format(msg, e1.getMsg());
            }
        }
        return msg;
    }



}